package com.ninetaildemonfox.zdl.socialproject.data.remote;

import android.content.Context;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.ResponseListener;
import com.kongzue.baseokhttp.util.JsonMap;
import com.kongzue.baseokhttp.util.JsonUtil;
import com.kongzue.baseokhttp.util.Parameter;
import com.ninetaildemonfox.zdl.socialproject.SpApplication;
import com.ninetaildemonfox.zdl.socialproject.data.TasksDataSource;
import com.ninetaildemonfox.zdl.socialproject.data.callback.LoadTaskCallback;

import rx.Subscription;

/**
 * Author: Mr.xiao on 2018/9/5
 *
 * @mail:xhb_199409@163.com
 * @github:https://github.com/xiaohaibin
 * @describe: 数据仓库代理类，用于封装从不同来源处获取数据
 */
public class TasksRepositoryProxy implements TasksDataSource {

    private static TasksRepositoryProxy INSTANCE = null;

    private TasksRepositoryProxy() {
    }

    public static TasksRepositoryProxy getInstance() {
        if (INSTANCE == null) {
            synchronized (TasksRepositoryProxy.class) {
                if (INSTANCE == null) {
                    return new TasksRepositoryProxy();
                }
            }
        }
        return INSTANCE;
    }

    @Override
    public void release() {
        INSTANCE = null;
    }


    @Override
    public Subscription login(Context me, String usrname, String pwd, final LoadTaskCallback<String> callback) {
        callback.onStart();
        HttpRequest.POST(SpApplication.getInstance().getBaseContext(), "", new Parameter(), new ResponseListener() {
            @Override
            public void onResponse(String response, Exception error) {
                callback.onCompleted();
                if (error == null) {
                    JsonMap jsonMap = JsonUtil.deCodeJsonObject(response);
                    String code = jsonMap.getString("code");
                    if (code.equals("1")) {
                        callback.onTaskLoaded(response);
                    } else if (code.equals("-1")) {

                    }
                }
            }
        });

        return null;
    }

}